package cn.qp.service.impl;

import cn.qp.pojo.Account;
import cn.qp.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author qipan
 * @date 2020/01/11 15:29
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Account> getAllAccounts() {
        return jdbcTemplate.query("select * from account", new RowMapper<Account>() {
            @Override
            public Account mapRow(ResultSet resultSet, int i) throws SQLException {
                final String username = resultSet.getString("username");
                final String balance = resultSet.getString("balance");
                final Account account = new Account();
                account.setUsername(username);
                account.setBalance(Double.valueOf(balance));
                return account;
            }
        });
    }
}
